//
//  AppSetting.m
//  Henkel
//
//  Created by Vergil on 2017/2/3.
//  Copyright © 2017年 Vergil. All rights reserved.
//

#import "AppSetting.h"
#import <SSKeychain/SSKeychain.h>

@interface AppSetting ()

@property (nonatomic, strong) NSUserDefaults *userDefaults;

@end

static AppSetting *appSetting = nil;

@implementation AppSetting

+ (instancetype)shareAppSetting {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        appSetting = [[AppSetting alloc] init];
    });
    return appSetting;
    
}

- (instancetype)init {
    
    if (self = [super init]) {
        _userDefaults = [NSUserDefaults standardUserDefaults];
    }
    return self;
    
}

#pragma mark - setting

- (void)setUserName:(NSString *)userName {
    
    [self.userDefaults setObject:userName forKey:USERNAME_KEY];
    
    [self.userDefaults synchronize];
    
}

- (void)setAccessToken:(NSString *)accessToken {
    
    [self.userDefaults setObject:accessToken forKey:ACCESS_TOKEN];
    
    [self.userDefaults synchronize];
    
}

- (void)setPassWord:(NSString *)passWord userName:(NSString *)userName {
    
    [SSKeychain setPassword:passWord forService:SERVICENAME account:userName];
    
}

- (void)setKeepLogin:(BOOL)keepLogin {
    
    [self.userDefaults setBool:keepLogin forKey:KEEPLOGIN_KEY];
    [self.userDefaults synchronize];
    
}

- (void)setAutoLogin:(BOOL)autoLogin {
    [self.userDefaults setBool:autoLogin forKey:AUTOLOGIN_KEY];
    [self.userDefaults synchronize];
}

- (void)setRememberPassword:(BOOL)rememberPassword {
    [self.userDefaults setBool:rememberPassword forKey:REMEMBER_PASSWORD];
    [self.userDefaults synchronize];
}

#pragma mark - getting

- (NSString *)userName {
    
    return [self.userDefaults objectForKey:USERNAME_KEY];
    
}

- (NSString *)accessToken {
    
    return [self.userDefaults objectForKey:ACCESS_TOKEN];
    
}

- (NSString *)passwordForUserName:(NSString *)userName {
    
    return [SSKeychain passwordForService:SERVICENAME account:userName];
    
}

- (BOOL)keepLogin {
    
    return [self.userDefaults boolForKey:KEEPLOGIN_KEY];
    
}

- (BOOL)autoLogin {
    
    return [self.userDefaults boolForKey:AUTOLOGIN_KEY];
    
}

- (BOOL)rememberPassword {
    
    return [self.userDefaults boolForKey:REMEMBER_PASSWORD];
    
}

@end
